<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 9/27/12
 * Time: 3:58 PM
 * To change this template use File | Settings | File Templates.
 */
class Controllers_Product extends Libs_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->menu=new Models_Menu();
        $this->getmenu();
        $this->model=new Models_Product();

    }
    public function getmenu()
    {

        $this->view->listmenusys=$this->menu->getmenusystem();
        $this->view->listmenuproduct=$this->menu->getmenuproduct();
        $this->view->listmanufacture=$this->menu->getmanufactory();
        $this->view->listnews=$this->menu->getnews();
    }
    public function index($param=null,$action=null)
    {

            $productperpage=1;
            $id=$param[0];
            if($param[1]=="") $pagenum=1;
            else
                $pagenum=$param[1];
            $this->view->action=$action;//lay ham
            $this->view->id=$id;
            $this->view->currenpage=$pagenum;//trang hien tai
            $this->view->listpro=$this->model->getproductbyfc($id,$pagenum,$productperpage);
            $totalpage=ceil($this->model->totalpage/$productperpage);
            $this->view->totalpage=$totalpage;
            $this->view->menupro=$this->model->getmenupro($id);
            $this->view->render('product/index');


    }
    public function viewall($param=null,$action=null)
    {
        $productperpage=1;
        $id=$param[0];
        if($param[1]=="") $pagenum=1;
        else
            $pagenum=$param[1];
        $this->view->action=$action;//lay ham
        $this->view->id=$id;
        $this->view->currenpage=$pagenum;//trang hien tai
        $this->view->listpro=$this->model->getproductbymenu($id,$pagenum,$productperpage);
        $totalpage=ceil($this->model->totalpage/$productperpage);
        $this->view->totalpage=$totalpage;
        $this->view->menupro=$this->model->getmenuviewall($id);
        $this->view->render('product/index');

    }
    public function detail($param=null)
    {
        if(isset($param[0]))
        {
            $pro=new Models_Product();
            $this->view->detailPro=$pro->getDetailProduct($param[0]);
            $this->view->render('product/detail');

        }
        else{
            echo 'Error';
        }
    }
    public function search($param=null)
    {
            if($_POST['btsearch']!='')
            {

                $pname=$_POST['pname'];
                $menupro=$_POST['menupro'];
                $manufactory=$_POST['manufactory'];

            }
            else{
                $pname=$param[0];
                $menupro=$param[1];
                $manufactory=$param[2];

            }
            $productperpage=1;


            if($param[3]=="") $pagenum=1;
            else
                $pagenum=$param[3];
            $this->view->currenpage=$pagenum;
            $this->view->listpro=$this->model->searchproduct($pname,$menupro,$manufactory,$pagenum,$productperpage);
            $totalpage=ceil($this->model->totalpage/$productperpage);
            $this->view->totalpage=$totalpage;
            $this->view->pname=$pname;
            $this->view->menupro=$menupro;
            $this->view->manufactory=$manufactory;
            $this->view->render('product/search');


    }

}
